<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopCouponTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('shop_coupon', ['comment' => '优惠券', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('name', 'string', ['limit' => 255, 'default' => '', 'comment' => '名称'])
            ->addColumn(Column::enum('type', ['reduce', 'discount'])->setDefault('reduce')->setComment('类型:reduce=满减券,discount=折扣券'))
            ->addColumn(Column::enum('use_scope', ['all_use', 'goods', 'disabled_goods', 'category'])->setDefault('all_use')->setComment('可用范围:all_use=全场通用,goods=指定商品可用,disabled_goods=指定商品不可用,category=指定分类可用'))
            ->addColumn('items', 'string', ['limit' => 255, 'null' => true, 'default' => null, 'comment' => '可用范围值'])
            ->addColumn('amount', 'decimal', ['precision' => 10,'scale' => 2, 'default' => 0, 'comment' => '券面额'])
            ->addColumn('max_amount', 'decimal', ['precision' => 10,'scale' => 2, 'default' => 0, 'comment' => '最大抵扣(折扣券)'])
            ->addColumn('enough', 'decimal', ['precision' => 10,'scale' => 2, 'default' => 0, 'comment' => '消费门槛'])
            ->addColumn('stock', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '库存'])
            ->addColumn('limit_num', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '每人限领'])
            ->addColumn('get_start_time', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '领取开始时间'])
            ->addColumn('get_end_time', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '领取结束时间'])
            ->addColumn(Column::enum('use_time_type', ['range', 'days'])->setDefault('range')->setComment('使用时间类型:range=固定区间,days=相对天数'))
            ->addColumn('use_start_time', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '使用开始时间'])
            ->addColumn('use_end_time', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '使用结束时间'])
            ->addColumn('start_days', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '开始有效天数'])
            ->addColumn('days', 'integer', ['limit' =>  10, 'default' => 0, 'comment' => '有效天数'])
            ->addColumn(Column::tinyInteger('is_double_discount')->setDefault(0)->setUnsigned()->setComment('优惠叠加:0=不可叠加,1=可叠加'))
            ->addColumn('description', 'string', ['limit' => 60, 'null' => true, 'default' => null, 'comment' => '描述'])
            ->addColumn(Column::enum('status', ['normal', 'hidden', 'disabled'])->setDefault('normal')->setComment('状态:normal=公开,hidden=后台发放,disabled=禁用'))
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addColumn('delete_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '删除时间'])
            ->create();


        $table = $this->table('shop_user_coupon', ['comment' => '用户优惠券', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('user_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '用户'])
            ->addColumn('coupon_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '优惠券'])
            ->addColumn('use_order_id', 'integer', ['limit' =>  11, 'default' => 0, 'comment' => '使用订单'])
            ->addColumn('use_time', 'integer', ['limit' =>  10, 'null' => true, 'default' => null, 'comment' => '使用时间'])
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addIndex('user_id')
            ->addIndex('coupon_id')
            ->addIndex('use_time')
            // ->addForeignKey('coupon_id', 'shop_coupon', 'id', ['delete' => 'CASCADE', 'update' => 'RESTRICT'])
            // ->addForeignKey('user_id', 'user', 'id', ['delete' => 'CASCADE', 'update' => 'RESTRICT'])
            ->create();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        // 优惠券
        $table = $this->table('shop_coupon');
        $table->drop();

        // 用户优惠券
        $table = $this->table('shop_user_coupon');
        $table->drop();
    }
}
